每一段SQL查詢結果都可被視為一個集合, 透過集合運算子進行資料集運算。集合邏輯可以搭配下方文氏圖理解:聯集代表A或者B的集合、交集代表是A也是B、差集代表是A但不是B
Example使用的table
with Travel as (
    select 'Amy' name, 'Japan' country
    union all
    select 'Oleve' name, 'Denmark' country
    union all
    select 'Jake' name, 'France' country
    union all
    select 'Luisa' name, 'Greece' country
    union all
    select 'Freddy' name, 'Japan' country
), Student as (
    select 'Amy' name, 123456 ID
    union all
    select 'Oleve' name, 567899 ID
    union all
    select 'Jake' name, 586966 ID
)
UNION / UNION ALL:聯集, 兩個查詢結果皆出現
select name from Travel t
union all
select name from Student t
;
-- output
Amy
Oleve
Jake
Luisa
Freddy
Amy -- 第2次出現
Oleve -- 第2次出現
Jake -- 第2次出現
select name from Travel t
union
select name from Student t
;
-- output
Amy
Oleve
Jake
Luisa
Freddy
Intersect:交集, 兩個查詢結果只出現重複項目
select name from Travel t
intersect
select name from Student t
;
-- output
Amy
Oleve
Jake
扣除查詢結果2 (即是查詢結果1, 但非查詢結果2)-- 查詢結果1
select name from Travel t
minus -- 替換成 except 結果相同, 但要看版次是否支援此語法
-- 查詢結果2
select name from Student t
;
-- output
Luisa
Freddy